package csp

import (
	"fmt"
	"sync"
	"time"
)

//生产者消费者模型

var wg sync.WaitGroup

func ProducerConsumerMode() {
	ch := make(chan int, 64)
	go Producer(3, ch)
	go Producer(5, ch)
	go Consumer(ch)

	time.Sleep(5 * time.Second)
}

func Producer(factor int, out chan<- int) {
	maxCount := 0
	for i := 0; ; i++ {
		fmt.Println("Producer: ", i)
		out <- i * factor
		maxCount++
		if maxCount > 10 {
			break
		}
	}
}

func Consumer(in <-chan int) {
	for v := range in {
		fmt.Println("Consumer: ", v)
	}
}

//func main() {
//	ProducerConsumerMode()
//}
